import requests
import datetime
import time
import smtplib
from email.mime.text import MIMEText
import re

def sleeptime(hour, min, sec):
    return hour * 3600 + min * 60 + sec
def emails1(t,i):
    msg_from = ''  # 发送方邮箱
    passwd = ''  # 填入发送方邮箱的授权码
    msg_to = ''  # 收件人邮箱
    subject = "冲鸭！快来挂号"  # 主题
    s1="监测程序的执行次数为："
    s2="当前时间为："
    s3 = "老哥，来不及解释了，快上车！王晞星大夫有号！快挂号,最近一天的号源为："  # 正文

    content=s1+str(t)+"\n"+s2+str(i)+"\n"+s3+num
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to
    try:
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)     # 邮件服务器及端口号
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg_to, msg.as_string())
        print("发送成功")
    except s.SMTPException:
        print("发送失败")
    finally:
        s.quit()
def emails2(t,i):
    msg_from = ''  # 发送方邮箱
    passwd = ''  # 填入发送方邮箱的授权码
    msg_to = ''  # 收件人邮箱
    subject = "别激动，现在没号，我只是证明一下我还在工作"  # 主题
    s1 = "监测程序的执行次数为："
    s2 = "当前时间为："
    s3 = "没号鸭！再等等呗"  # 正文
    content = s1 + str(t) +"\n" +s2 +str(i) +"\n"+ s3
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to
    try:
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)     # 邮件服务器及端口号
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg_to, msg.as_string())
        print("发送成功")
    except s.SMTPException:
        print("发送失败")
    finally:
        s.quit()

def matching(s):
    # 正则表达式匹配特定字符后面的一个或两个数字，优先匹配两个数字。
    pattern = re.compile(
        r'\\xa1\\xa1\\xca\\xa3\\xd3\\xe0\\xd4\\xa4\\xd4\\xbc\\xc1\\xbf\\xa3\\xba\d\d|\\xa1\\xa1\\xca\\xa3\\xd3\\xe0\\xd4\\xa4\\xd4\\xbc\\xc1\\xbf\\xa3\\xba\d')
    result = re.findall(pattern, s)
    pattern2 = re.compile(r'\d\d$|\d$')
    x = 0

    while x < len(result):
        result2 = re.findall(pattern2, result[x])
        x=x+1
        if result2[0]!= '0':
            return result2[0]

    # result2=re.findall(pattern2,result)
    # 如果1匹配成功


head = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Cookie':'UM_distinctid=176dbef3815137-0ae144efe22441-33524d7c-1fa400-176dbef3816ac9; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22176dc0ddcdf6ad-0834622b2b3071-33524d7c-2073600-176dc0ddce05ec%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22176dc0ddcdf6ad-0834622b2b3071-33524d7c-2073600-176dc0ddce05ec%22%7D; alicfw_gfver=v1.200309.1; alicfw=3900267242%7C1121275208%7C1328233921%7C1328232945; cityid=; touch=; ASPSESSIONIDAARQCQAR=MBCHAPJBNMLBGOJIEFHDGOEI; CNZZDATA1255857050=1136038365-1610003000-http%253A%252F%252Fwww.sxyygh.com%252F%7C1610316905',
    'Host':'www.sxyygh.com',
    'Referer':'http://www.sxyygh.com/gh/gh_search.asp',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
}

r = requests.get('http://www.sxyygh.com/gh/doctor.asp?id=109007', headers=head)
r.encoding='unicode_escape'


s=str(r.content)
num=matching(s)

# print("可预约号的数量为：%s"%(num))
second = sleeptime(0, 0, 10)
t=1

while 1 == 1:
    time.sleep(second)
    if num==None:
        i = datetime.datetime.now()
        print("*****************************************************")
        print("监测程序的第%d次执行"%(t))
        print("当前的日期和时间是 %s" % i)
        print("王晞星大夫目前有号源的天数为0,再等等吧！")
        print("*****************************************************")
        t=t+1
        if t%360==0:
            emails2(t,i)
    else:
        i = datetime.datetime.now()
        print("*****************************************************")
        print("监测程序的第%d次执行" % (t))
        print("当前的日期和时间是 %s" % i)
        print("王晞星大夫最近一天的出诊号源数为：%s,立即抢号！！"%(num))
        print("*****************************************************")
        #emails1(t,i)
        t=t+1

